namespace DataAccess.Base
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using DataAccess.Handlers;
    using System.ComponentModel;

    /// <summary>
    /// Clase DAO base para stored procedures que devuelven una tabla (se mapea a un listado de entidades).
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [DataObject(true)]
    public class ProcedureTableDAO<T> : ProcedureDAO<T> where T : new()
    {
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public IList<T> Execute()
        {
            this.PopulateParameters();

            DataTable dt = DataHelper.Current.ExecuteDataTable(this.ProcedureName, this.Parameters);
            
            List<T> entities = new List<T>();
            T entity;

            foreach (DataRow r in dt.Rows)
            {
                entity = EntityDataMapper.MapToEntity<T>(r);
                entities.Add(entity);
                DataHandlers.Current.PostRetrieve.Process(entity, r, this.ProcedureName);
            }

            return entities;
        }
    }
}
